package com.yiqi.liebang.feature.people.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.suozhang.framework.component.http.RxDataProcessFactory;
import com.suozhang.framework.component.recyclerview.DividerItemDecoration;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.framework.Callback;
import com.suozhang.framework.widget.EmptyView;
import com.yiqi.liebang.R;
import com.yiqi.liebang.common.util.RxKeyboardTool;
import com.yiqi.liebang.entity.bo.FriendBo;
import com.yiqi.liebang.entity.bo.SearchFriendBo;
import com.yiqi.liebang.feature.mine.view.UserInfoActivity;
import com.yiqi.liebang.feature.people.contract.PeopleContract;
import com.yiqi.liebang.feature.people.dependencies.people.DaggerPeopleComponent;
import com.yiqi.liebang.feature.people.dependencies.people.PeoplePresenterModule;
import com.yiqi.liebang.feature.people.view.adapter.FriendsAdapter;
import com.yiqi.liebang.framework.api.HomeApi;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class PeopleActivity extends BaseActivity implements BaseQuickAdapter.OnItemClickListener, PeopleContract.View, TextWatcher, BaseQuickAdapter.OnItemChildClickListener, TextView.OnEditorActionListener {


    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.rv_people)
    RecyclerView mRvPeople;

    @Inject
    PeopleContract.Presenter mPresenter;
    @BindView(R.id.edt_people)
    EditText mEdtPeople;
    FriendsAdapter mAdapter;
    EmptyView mEmptyView;
    EmptyView mEmptyViewTJ;
    @BindView(R.id.rv_gxq)
    RecyclerView mRvGxq;
    private com.yiqi.liebang.feature.home.view.adapter.FriendsAdapter questionAdapter;

    @Override
    protected void onStart() {
        super.onStart();
        RxKeyboardTool.showSoftInput(this, mEdtPeople);
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_people;
    }

    @Override
    protected void initInjector() {
        DaggerPeopleComponent.builder().peoplePresenterModule(new PeoplePresenterModule(this)).build().inject(this);
    }


    @Override
    protected void initView() {

        initToolBar(mToolbar, "拓展人脉", true, true);
        initAdapter();
        initxqAdapter();
    }

    @Override
    protected void initData() {
        getData();
    }

    private void initAdapter() {
        mEmptyView = new EmptyView(mRvPeople);
        mRvPeople.setLayoutManager(new LinearLayoutManager(this));
        mRvPeople.addItemDecoration(new DividerItemDecoration(DividerItemDecoration.VERTICAL).setHeight(2));
        mAdapter = new FriendsAdapter(true);
        mAdapter.bindToRecyclerView(mRvPeople);
        mAdapter.setOnItemClickListener(this);
        mAdapter.setOnItemChildClickListener(this);
        mAdapter.setCommonCallback(new Callback.CommonCallback() {
            @Override
            public void onSubscribe(Disposable d) {
                showLoading(d);
            }

            @Override
            public void onSuccess(Object result) {
                dismissLoading();
            }

            @Override
            public void onError(Throwable ex) {
                dismissLoading();
            }

            @Override
            public void onComplete() {
                dismissLoading();
            }
        });

    }

    @Override
    protected void initEvent() {
        super.initEvent();
        mEdtPeople.setOnEditorActionListener(this);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Intent intent = new Intent(this, UserInfoActivity.class);
        intent.putExtra("is_view_my", false);
        intent.putExtra("visitorUid", mAdapter.getData().get(position).getUserUid());
        startActivity(intent);
    }

    @Override
    public void showResetSuccess(FriendBo s) {
        mAdapter.setNewData(s.getFriend());

    }

    @Override
    public void showFriendsEmpty() {
        mAdapter.setEmptyView(mEmptyView.getEmptyView("暂无搜索到匹配好友"));
        mAdapter.setNewData(null);
    }

    @Override
    public void showApplySuccess() {
        showMsg("好友请求发送成功！");
    }

    @Override
    public void showPeopleEmpty() {
        showMsg("暂无搜索到匹配好友！");
    }

    @Override
    public void showError(String error) {

        mAdapter.setEmptyView(mEmptyView.getErrorView(error));
        mAdapter.setNewData(null);

    }


    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        if (AM.user().isLogin()) {

        }

    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {

    }

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {//搜索按键action
            RxKeyboardTool.hideSoftInput(PeopleActivity.this);
            String content = mEdtPeople.getText().toString();
            if (TextUtils.isEmpty(content)) {
                showMsg("请输入搜索内容！");
                return true;
            }
            mPresenter.searchPeople(content);
            return true;
        }
        return false;
    }

    private void getData() {
        AM.api().createApiService(HomeApi.class).tuijianFriends()
                .compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer())
                .subscribe(new Observer<List<SearchFriendBo.UserFriendBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        showLoading(d);
                    }

                    @Override
                    public void onNext(List<SearchFriendBo.UserFriendBean> friendBeans) {

                        dismissLoading();
                        if (friendBeans != null && friendBeans.size() > 0) {
                            questionAdapter.setNewData(friendBeans);
                        } else {
                            questionAdapter.setEmptyView(mEmptyViewTJ.getEmptyView("暂无推荐好友"));
                            questionAdapter.setNewData(null);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        dismissLoading();
                        questionAdapter.setEmptyView(mEmptyViewTJ.getErrorView(e.getMessage()));
                        questionAdapter.setNewData(null);
                    }

                    @Override
                    public void onComplete() {

                        dismissLoading();
                    }
                });
    }

    private void initxqAdapter() {
        mEmptyViewTJ = new EmptyView(mRvPeople);
        mRvGxq.setLayoutManager(new LinearLayoutManager(this));
        mRvGxq.addItemDecoration(new DividerItemDecoration(DividerItemDecoration.VERTICAL).setHeight(2));
        questionAdapter = new com.yiqi.liebang.feature.home.view.adapter.FriendsAdapter(true, this);
        questionAdapter.bindToRecyclerView(mRvGxq);
        questionAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(PeopleActivity.this, UserInfoActivity.class);
                intent.putExtra("is_view_my", false);
                intent.putExtra("visitorUid", questionAdapter.getData().get(position).getUserUid());
                startActivity(intent);
            }
        });
        questionAdapter.setOnItemChildClickListener(this);
        questionAdapter.setCommonCallback(new Callback.CommonCallback() {
            @Override
            public void onSubscribe(Disposable d) {
                showLoading(d);
            }

            @Override
            public void onSuccess(Object result) {
                dismissLoading();
            }

            @Override
            public void onError(Throwable ex) {
                dismissLoading();
            }

            @Override
            public void onComplete() {
                dismissLoading();
            }
        });

    }


    @OnClick(R.id.btn_search_people)
    public void onViewClicked() {
        String content = mEdtPeople.getText().toString();
        if (TextUtils.isEmpty(content)) {
            showMsg("请输入搜索内容！");
            return;
        }
        mPresenter.searchPeople(content);
    }
}
